package com.example.demo.component;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-08-24
 * Time: 14:37
 */
@ControllerAdvice
public class ExceptionAdvice {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Object exceptionAdvice(Exception e) {
        Map<String, Object> map = new HashMap<String, Object>(){{
            put("success", -1);
            put("message", "总的异常信息：" + e.getMessage());
            put("data", null);
        }};
        return map;
    }
    @ExceptionHandler(NullPointerException.class)
    @ResponseBody
    public Object nullPointerException(Exception e) {
        Map<String, Object> map = new HashMap<String, Object>(){{
            put("success", -1);
            put("message", "空指针异常：" + e.getMessage());
            put("data", null);
        }};
        return map;
    }

}
